<?php
/**
 *   无限级评论回复功能
 * @param
 * @date 2019/12/6
 * @author Xiaohui Zeng
 */

namespace app\blog\controller;


use app\model\CmsCommentReply;
use app\util\ReturnCode;

class CommentReply extends Base
{
    /**
     *   获取评论回复列表
     * @param $wid 文章id
     * @date 2019/12/6
     * @author Xiaohui Zeng
     */
    public function index()
    {
        $wid = $this->request->get('wid');
        $page = $this->request->get('page', 1);
        $pageSize = $this->request->get('pageSize', 6);
        if (strlen($pageSize)) {
            $limit = $pageSize;
        } else {
            $limit = config('apiAdmin.ADMIN_LIST_DEFAULT');
        }
        if (!strlen($wid)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, '确实必要参数');
        }
        $objData = new CmsCommentReply();
        if (strlen($wid)) {
            $obj = $objData->where(['wid' => $wid, 'hide' => 1, 'ischeck' => 1]);
        }
        $CmsCommentReplyList = $obj->order('createTime ASC')->paginate($limit, false, ['page' => $page])->toArray();
        return $this->buildSuccess([
            'list'  => $CmsCommentReplyList['data'],
            'count' => $CmsCommentReplyList['total']
        ], '获取成功');
    }

    /**
     *   新增
     * @param
     * @date 2019/12/6
     * @author Xiaohui Zeng
     */
    public function add()
    {
        $postData = $this->request->post();
        // 增加新增时间字段
//        date_default_timezone_set('PRC');
        $postData['createTime'] = date('Y-m-d H:i:s', time());
        $postData['ip'] = $this->request->ip();//$this->request->ip();112.11.49.78
        if($postData['ip'] == "127.0.0.1") {
            // 如果是本地调试
            $postData['ip'] = "112.11.49.78";
        }
        $ips = $this->getIp($postData['ip']);
        if(!empty($ips)) {
            if ($ips->status == "1") {
                $postData['cityName'] = $ips->province . $ips->city;
            }
        }
        $res = new CmsCommentReply($postData);
        // 过滤post数组中的非数据表字段数据
        $res->allowField(true)->save();
        //$res->id; // 自增id
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '新增失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     *   点赞
     * @param
     * @date 2019/12/6
     * @author Xiaohui Zeng
     */
    public function like()
    {
        $id = $this->request->get('id');
        if(strlen($id)) {
            $w = CmsCommentReply::get($id);
            $w->likenum= ['inc', 1];
            $w->save();
            return $this->buildSuccess([
                'likenum'  => CmsCommentReply::get($id)->likenum
            ]);
        }
    }

    public function getIp($ip){

        $url ="https://restapi.amap.com/v3/ip?ip=$ip&output=JSON&key=2029d778da029046575131dcc49ef285";
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//绕过ssl验证

        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return json_decode($output);
    }
}